- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path529. Minesweeper.go
46 lines (42 loc) · 844 Bytes
/
529. Minesweeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package leetcode
vardir8= [][]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{1, 1},
{1, 0},
{1, -1},
{0, -1},
}
funcupdateBoard(board [][]byte, click []int) [][]byte {
ifboard[click[0]][click[1]] =='M' {
board[click[0]][click[1]] ='X'
returnboard
}
dfs(board, click[0], click[1])
returnboard
}
funcdfs(board [][]byte, x, yint) {
cnt:=0
fori:=0; i<8; i++ {
nx, ny:=x+dir8[i][0], y+dir8[i][1]
ifisInBoard(board, nx, ny) &&board[nx][ny] =='M' {
cnt++
}
}
ifcnt>0 {
board[x][y] =byte(cnt+'0')
return
}
board[x][y] ='B'
fori:=0; i<8; i++ {
nx, ny:=x+dir8[i][0], y+dir8[i][1]
ifisInBoard(board, nx, ny) &&board[nx][ny] !='B' {
dfs(board, nx, ny)
}
}
}
funcisInBoard(board [][]byte, x, yint) bool {
returnx>=0&&x<len(board) &&y>=0&&y<len(board[0])
}